package com.weeego.util.file;

import java.io.*;
import java.security.*;

public class HashFile {
    public static char[] hexChar = { '0', '1', '2', '3', '4', '5', '6', '7',
	    '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    public static void main(String[] args) throws Exception {
	String fileName = "D:\\text1.txt";
	String hashType = "MD5";
	System.out.println(hashType + " == " + getHash(fileName, hashType));
	hashType = "SHA1";
	System.out.println(hashType + " == " + getHash(fileName, hashType));
	hashType = "SHA-256";
	System.out.println(hashType + " == " + getHash(fileName, hashType));
	hashType = "SHA-384";
	System.out.println(hashType + " == " + getHash(fileName, hashType));
	hashType = "SHA-512";
	System.out.println(hashType + " == " + getHash(fileName, hashType));
	
	hashType = "MD5";
	System.out.println(hashType + " == " + getHash(new FileInputStream("temp/filename.xml"), hashType));

    }

    public static String getHash(FileInputStream fis, String hashType) {

	byte[] buffer = new byte[1024];
	MessageDigest md5;
	try {
	    md5 = MessageDigest.getInstance(hashType);
	    int numRead = 0;
	    while ((numRead = fis.read(buffer)) > 0) {
		md5.update(buffer, 0, numRead);
	    }
	    fis.close();
	    return toHexString(md5.digest());
	} catch (IOException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	} catch (NoSuchAlgorithmException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}
	return null;
    }

    public static String getHash(String fileName, String hashType)
	    throws Exception {
	InputStream fis;
	fis = new FileInputStream(fileName);
	byte[] buffer = new byte[1024];
	MessageDigest md5 = MessageDigest.getInstance(hashType);
	int numRead = 0;
	while ((numRead = fis.read(buffer)) > 0) {
	    md5.update(buffer, 0, numRead);
	}
	fis.close();
	return toHexString(md5.digest());
    }

    public static String toHexString(byte[] b) {
	StringBuilder sb = new StringBuilder(b.length * 2);
	for (int i = 0; i < b.length; i++) {
	    sb.append(hexChar[(b[i] & 0xf0) >>> 4]);
	    sb.append(hexChar[b[i] & 0x0f]);
	}
	return sb.toString();
    }
    
    
    
    
    
    
}